import Toast from 'react-native-root-toast';
import {Keyboard,Clipboard,NativeModules,NativeEventEmitter} from 'react-native';
import { isAndroid, isIOS } from "../configs/platform";
import BaseComponent from './common/BaseComponent';
import NetInfo from '@react-native-community/netinfo';
import navigator from '../utils/navigator';
import {addLocationListener, stop} from "react-native-amap-geolocation";
import { geo2Adr } from "../utils/util";


/**
 * 基类
 */
class BasePage extends BaseComponent {

  constructor(props) {
    super(props);
    this.keyBoardIsShow = false;
    this.hasLocate=false;
  }

  componentWillMount() {
    this.keyboardDidShowListener = Keyboard.addListener('keyboardDidShow', this._keyboardDidShow);
    this.keyboardDidHideListener = Keyboard.addListener('keyboardDidHide', this._keyboardDidHide);
  }

  componentDidMount() {
   this.netListener= NetInfo.addEventListener(state => {
      if (!state.isInternetReachable && !state.isConnected) {
        // this.jumpToPage('NetError');
        global.netState = "无网络连接";
      }
      else{
        global.netState = "正常";
      }
    });


    // this.callListener = call.addListener('callListener',(state)=>{
    //   console.log("系统来电状态1：===》 ",state);
    // });
  }

  _keyboardDidShow = () => {
    this.keyBoardIsShow = true;
  };

  _keyboardDidHide = () => {
    this.keyBoardIsShow = true;
  };

  lostBlur = () => {
    Log('lostBlur:' + this.keyBoardIsShow);
    //退出软件盘
    if (this.keyBoardIsShow) {
      Keyboard.dismiss();
    }
  };

  handleSchemeListener = (schemeStr) => {
  };


  getUrlParam(href, name) {
    let _href = href;
    let url = href.substring(_href + 1, href.length);
    let urlArray = url.split('&');
    for (let i = 0; i < urlArray.length; i++) {
      let _url = urlArray[i].split('=');
      for (let j = 0; j < _url.length; j++) {
        if (name === _url[j]) {
          return _url[j + 1];
        }
      }
    }
  }

  componentWillUnmount() {
    try {
      this.netListener&&this.netListener.remove();
      this.keyboardDidShowListener.remove();
      this.keyboardDidHideListener.remove();
      this.locationListener && this.locationListener.remove();
    } catch (e) {
    }
  }

  toast = null;
  showToast = (message, position = Toast.positions.CENTER) => {
    this.toast && this.toast.destroy();
    this.toast = Toast.show(message, {
      position,
      onHidden: () => {
        this.toast.destroy();
        this.toast = null;
      },
    });
  };

  /**
   * 返回
   * @private
   */
  _onBackPress = () => {
    navigator.goBack();
  };

  /**
   * 跳转页面
   * @param page
   */
  jumpToPage = (page,params={}) => {
    try {
      Log('跳转', page);
      navigator.navigate(page,params);
    } catch (e) {
      Log('跳转错误', e);
    }

  };


  showSinglePicView = (url) => {
    const images = [];
    images.push(url);
    if (isAndroid()) {
      previewPic(images);
    } else {
      this.zomImageViewer && this.zomImageViewer.show(images);
    }
  };

  showMultiplePicView = (images, position) => {
    if (isAndroid()) {
      previewPic(images, position);
    } else {
      this.zomImageViewer && this.zomImageViewer.show(images, position);
    }
  };


  addLocationListener = () => {
    this.locationListener = addLocationListener(async cityInfo => {
      // logger('获取定位信息', cityInfo,this.hasLocate)
      if(!IsNullOrEmpty(cityInfo))
      {
        this.props.userStore.setValue('location', cityInfo)
      }
      // if (!IsNullOrEmpty(cityInfo)&&this.hasLocate==false) {
      //
      //     if (IsNullOrEmpty(cityInfo.province)) {
      //       logger('获取定位信息2', cityInfo.latitude, cityInfo.longitude)
      //         const ret = await geo2Adr(cityInfo.latitude, cityInfo.longitude).catch(e => Log(e));
      //         if (!IsNullOrEmpty(ret)) {
      //             this.props.userStore.setValue('location', ret)
      //             this.hasLocate=true
      //             if(!IsNullOrEmpty(location)&&callBack&&location.city!=cityInfo.city){
      //               callBack&&callBack();
      //             }
      //         }
      //     } else {
      //         this.props.userStore.setValue('location', cityInfo)
      //         this.hasLocate=true
      //         if(!IsNullOrEmpty(location)&&callBack&&location.city!=cityInfo.city){
      //           callBack&&callBack();
      //         }
      //     }
      // }
    })
  }
}


export default BasePage;
